package com.tinyideatech.admin.controller.car;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.core.text.Convert;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.system.entity.CarModel;
import com.tinyideatech.system.service.ICarModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author zhangy-5
 * @date 2019-09-09
 */

@Controller
@RequestMapping("/car/model")
public class CarModelController extends BaseController {

    @Autowired
    private ICarModelService carModelService;

    @GetMapping()
    public String list() {
        return "car/model/list";
    }

    @GetMapping("/add")
    public String add() {
        return "car/model/add";
    }

    @GetMapping("/edit/{id}")
    public String carModelEdit(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        CarModel carModel = carModelService.getById(id);
        map.addAttribute("carModel", carModel);
        return "car/model/edit";
    }

    @ResponseBody
    @PostMapping("/list")
    public PageResult list(CarModel carModel) {
        QueryWrapper<CarModel> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty("")) {
            qw.like("", "");
        }
        IPage<CarModel> page = startPage();
        IPage<CarModel> carModelList = carModelService.page(page, qw);
        return page(carModelList);
    }

    @ResponseBody
    @PostMapping("/save")
    public ApiResult save(
            CarModel sysRole
    ) {
        carModelService.saveOrUpdate(sysRole);
        return success();
    }

    @ResponseBody
    @PostMapping("/remove")
    public ApiResult remove(
            String ids
    ) {
        Long[] carModelIds = Convert.toLongArray(ids);
        carModelService.removeByIds(Arrays.asList(carModelIds));
        return success();
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public ApiResult changeStatus(CarModel carModel) {
        carModelService.changeStatus(carModel);
        return success();
    }

}
